<template>
  <ModalTemplate @close="$emit('close')">
    <div class="modal-header" slot="header">
      <h2 class="inline">作业筛选</h2>
      <span>
        人员：
        <Select
          class="select-people"
          filterable
          v-model="filterPeople"
          :disabled="isLockPeople"
        >
          <Option value="all">所有人</Option>
          <Option
            v-for="(item, index) in members"
            :key="'member_' + index"
            :value="item.qq"
            >{{ item.nickname }}</Option
          >
        </Select>
      </span>
      <span class="mar10l">
        题目：
        <Select
          class="w100"
          filterable
          v-model="filterQuestion"
          :disabled="isLockQuestion"
        >
          <Option value="all">所有</Option>
          <Option
            v-for="(item, index) in questions"
            :key="'q_' + index"
            :value="item.name"
            >{{ item.name }}</Option
          >
        </Select>
      </span>
      <span class="mar10l">
        关键词：
        <Input
          style="min-width: 100px; width: auto; display: inline-block"
          v-model="searchKey"
        ></Input>
      </span>
      <div class="right">
        <Button @click="doExportList">导出</Button>
      </div>
      <div class="clear"></div>
    </div>
    <div class="people-work-list">
      <dl v-for="(item, index) in showList" :key="'show_' + index">
        <dt>
          <template v-if="isShowNO"> 编号：{{ qqObj[item.qq].no }} </template>
          <template v-else>{{ item.title }}</template>
        </dt>
        <dd v-html="showContent(item.content)"></dd>
      </dl>
      <Spin fix v-if="isLoading"></Spin>
    </div>
    <div slot="footer">
      <span class="left">总数：{{ showList.length }}条</span>
      <Button @click="$emit('close')">返回</Button>
    </div>
  </ModalTemplate>
</template>

<script>
import { splitPerson, splitQuetions, format2Html, saveTXT } from "js/utils.js";
export default {
  props: ["filterObj"],
  data() {
    return {
      isShow: true,
      isLoading: false,
      allList: [],
      questions: [],
      showList: [],
      // 筛选条件
      filterPeople: "all",
      filterQuestion: "all",
      searchKey: "",
    };
  },
  computed: {
    isShowNO() {
      return this.filterObj && this.filterObj.isShowNO;
    },
    isLockPeople() {
      return this.filterObj && this.filterObj.isLockPeople;
    },
    isLockQuestion() {
      return this.filterObj && this.filterObj.isLockQuestion;
    },
    members() {
      return this.$store.getters.getMembers();
    },
    qqObj() {
      return this.$store.getters.getQQObj();
    },
    nickNameObj() {
      return this.$store.getters.getNickNameObj();
    },
    filterParams() {
      return {
        people: this.filterPeople,
        question: this.filterQuestion,
        searchKey: this.searchKey,
      };
    },
  },
  watch: {
    filterParams() {
      this.doFilter();
    },
  },
  methods: {
    async getAllList() {
      if (this.allList.length !== 0) {
        return;
      }
      if (this.members.length === 0) {
        this.$global.log("请先导入成员");
      }
      this.isLoading = true;
      let text = await this.$global.getFileText("base");
      if (!text) {
        this.$global.log("请选择大群文件");
        this.isLoading = false;
        return;
      }
      let allList = splitPerson(text);
      let searchList = [];
      let qObjs = {};
      for (let i = 0, n = allList.length; i < n; i++) {
        let item = allList[i];
        let content = allList[i].content;
        if (!content) {
          continue;
        }
        if (item.qq) {
          if (!this.qqObj[item.qq]) {
            continue;
          }
          if (item.nickname !== this.qqObj[item.qq].nickname) {
            item.nickname = this.qqObj[item.qq].nickname;
          }
        } else {
          if (!this.nickNameObj[item.nickname]) {
            continue;
          }
          item.qq = this.nickNameObj[item.nickname].qq;
        }
        // 过滤题目
        let qas = splitQuetions(content);
        for (let j = 0, m = qas.length; j < m; j++) {
          let qa = qas[j];
          let qList = [];
          if (qObjs[qa.name]) {
            qList = qObjs[qa.name];
          }
          if (!qList.includes(qa.question)) {
            qList.push(qa.question);
          }
          qObjs[qa.name] = qList;
        }
        item.qas = qas;
        if (qas.length === 0) {
          continue;
        }
        searchList.push(item);
      }
      this.allList = searchList;
      let questions = [];
      for (let name in qObjs) {
        questions.push({
          name: name,
          list: qObjs[name],
        });
      }
      this.questions = questions;
      this.isLoading = false;
      this.doFilter();
    },
    // 筛选
    doFilter() {
      this.isLoading = true;
      let list = this.allList;
      let params = this.filterParams;
      let people = params.people;
      let question = params.question;
      let searchKey = params.searchKey;
      if (people === "all") {
        people = "";
      }
      if (question === "all") {
        question = "";
      }
      if (people) {
        list = list.filter((item) => item.qq === people);
      }
      if (question) {
        list = list.filter((item) => {
          let names = item.qas.map((qa) => qa.name);
          return names.includes(question);
        });
      }
      if (searchKey) {
        list = list.filter((item) => {
          if (question) {
            let answer = item.qas.filter((qa) => qa.name === question)[0]
              .answer;
            return answer.indexOf(searchKey) > -1;
          } else {
            return item.content.indexOf(searchKey) > -1;
          }
        });
      }

      this.showList = list;
      this.isLoading = false;
    },
    // 导出
    doExportList() {
      let fileName = "作业记录.txt";
      let str = "";
      for (let i = 0, n = this.showList.length; i < n; i++) {
        let item = this.showList[i];
        str += "\n" + item.title + "\n" + item.content;
      }
      saveTXT(str, fileName);
    },
    // 显示详情
    showContent(content) {
      if (!content) {
        return "";
      }
      let str = content.trim();
      let qas = splitQuetions(str);
      if (this.filterQuestion) {
        for (let i = 0, n = qas.length; i < n; i++) {
          let qa = qas[i];
          if (qa.name === this.filterQuestion) {
            let answer = qa.answer.trim();
            str = `<b>${qa.question}</b>
            ${answer}`;
          }
        }
      } else {
        for (let i = 0, n = qas.length; i < n; i++) {
          let question = qas[i].question;
          str = str.replace(question, "<b>" + question + "</b>");
        }
      }
      str = str.trim();
      if (this.searchKey) {
        let reg = new RegExp(this.searchKey, "g");
        if (reg.test(str)) {
          str = str.replace(
            reg,
            "<span class='highlight weight'>" + this.searchKey + "</span>"
          );
        }
      }
      str = format2Html(str);
      return str;
    },
  },
  async mounted() {
    await this.getAllList();
    if (this.filterObj) {
      this.filterPeople = this.filterObj.people || "all";
      this.filterQuestion = this.filterObj.question || "all";
      this.searchKey = this.filterObj.searchKey || "";
    }
  },
};
</script>
